package sing.core.dialog

import android.app.Activity
import android.widget.TextView
import androidx.appcompat.app.AlertDialog
import sing.core.callback.Callback
import sing.core.util.ToSettingUtil

object ToOpenPermission {

    fun requestDialog(activity: Activity, str: String, callback: Callback? = null, toSetting:Boolean = true, isLocation:Boolean = false){
        val dialog = CustomDialog(activity)
        dialog.setCancelable(false)
        dialog.setConfirm("去开启")
        val tv = TextView(activity)
        tv.text = "请允许APP获取您的${str}权限"
        dialog.setView(tv)
        dialog.addListener(object : CustomDialog.OnClickListener{
            override fun cancel(dialog: AlertDialog?) {
                super.cancel(dialog)
                callback?.result(0)
            }

            override fun confirm(dialog: AlertDialog?) {
                super.confirm(dialog)
                callback?.result(1)
                if (toSetting){
                    if (isLocation){
                        if (ToSettingUtil.isOPen(activity) ){
                            ToSettingUtil.toPermission(activity)
                        }else{
                            ToSettingUtil.openGPS(activity)
                        }
                    }else{
                        ToSettingUtil.toPermission(activity)
                    }
                }
            }
        })
        dialog.show()
    }
}